package com.example.administrator.activity.parcitise.Activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.activity.R;

public class MarketListActivity extends AppCompatActivity {

    private ListView listView;

    /**
     * 1.Ch2MarketActivity使用的布局文件：LIstView
     * 2.列表中每一项显示的内容，使用另一个布局文件，ImageView+
     * 3.
     *
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_market_list);
        listView = findViewById(R.id.activity_market);
        final MyAdapter adapter = new MyAdapter();
        listView.setAdapter(adapter);

        AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String s = adapter.name[position];
                Toast.makeText(MarketListActivity.this, s, Toast.LENGTH_SHORT).show();
            }
        };

    }

    class MyAdapter extends BaseAdapter {
        /**
         * 定义列表项要显示的图片和文字数组
         */
        int[] img = {R.drawable.jd, R.drawable.qq, R.drawable.dz, R.drawable.xl, R.drawable.uc, R.drawable.tm, R.drawable.wx};
        String[] name = {"京东", "QQ", "QQ斗地主", "新浪微博", "UC浏览器", "天猫商城", "微信"};

        /**
         * 得到 列表的项数/总和
         *
         * @return
         */
        @Override
        public int getCount() {
            return name.length;
        }

        /**
         * 得到列表项
         *
         * @param position
         * @return
         */
        @Override
        public Object getItem(int position) {
            return name[position];
        }

        /**
         * 得到列表项id（第几项）
         *
         * @param position
         * @return
         */
        @Override
        public long getItemId(int position) {
            return position;
        }

        /**
         * 得到列表View视图
         *
         * @param position
         * @param convertView
         * @param parent
         * @return
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
//            /**
//             * 把内容（图片+文字  数组）展示在控件上（imageView+TextView）
//             * 1.关联布局文件
//             * 2.关联控件
//             * 3.给控件设置内容 set
//             */
//            View view=View.inflate(MarketListActivity.this,R.layout.activity_market_list__item,null);
//            ImageView imageView=view.findViewById(R.id.activity_market_jd);
//            TextView textView=view.findViewById(R.id.activity_market_jd_text);
//
//            imageView.setImageResource(img[position]);
//            textView.setText(name[position]);
//            return view;
            /**
             * 数据量过大时，为了LIstView减少耗时操作，利用
             */
            ViewHolder viewHolder = new ViewHolder();
            if (convertView == null) {
                convertView = View.inflate(MarketListActivity.this, R.layout.activity_market_list_item, null);
                viewHolder.imageView = convertView.findViewById(R.id.activity_market_jd);
                viewHolder.textView = convertView.findViewById(R.id.activity_market_jd_text);
                convertView.setTag(viewHolder);

            }else {
                viewHolder= (ViewHolder) convertView.getTag();
            }
            viewHolder.imageView.setImageResource(img[position]);
            viewHolder.textView.setText(name[position]);
            return convertView;
        }
    }

    static class ViewHolder {
        ImageView imageView;
        TextView textView;
    }

}
